package Views;

import Controllers.MachineCTR;
import Models.Machine;
import javax.swing.JOptionPane;

/**
 * @author Tanja
 */
public class MachineInfoView extends javax.swing.JFrame
{
	private Machine machine;
	private MachineCTR controller = new MachineCTR();

    public MachineInfoView(Machine machine)
	{
		this.machine = machine;
        initComponents();
		nameTF.setText(this.machine.getName());
		if(this.machine.isAvailable() == true)
		{
			availableYesRB.setSelected(true);
		}
		else
		{
			availableNoRB.setSelected(true);
		}
		if(!machine.isActivated())
		{
			activatedCB.setSelected(false);
		}

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        availabilityBG = new javax.swing.ButtonGroup();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        availabilityLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        availableYesRB = new javax.swing.JRadioButton();
        availableNoRB = new javax.swing.JRadioButton();
        editButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        descriptionLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Maskineinformation");

        nameLabel.setText("Navn:");

        nameTF.setEnabled(false);

        availabilityLabel.setText("Tilgængelig:");

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        availabilityBG.add(availableYesRB);
        availableYesRB.setText("Ja");
        availableYesRB.setEnabled(false);

        availabilityBG.add(availableNoRB);
        availableNoRB.setText("Nej");
        availableNoRB.setEnabled(false);

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Slet");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        descriptionLabel.setText("Oversigt over den valgte maskine");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(nameLabel)
                            .add(availabilityLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(availableYesRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(availableNoRB))
                            .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 157, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(editButton)
                            .add(18, 18, 18)
                            .add(removeButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(closeButton))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(descriptionLabel)
                            .add(47, 47, 47)
                            .add(activatedCB))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(descriptionLabel)
                    .add(activatedCB))
                .add(9, 9, 9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(9, 9, 9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(availabilityLabel)
                    .add(availableYesRB)
                    .add(availableNoRB))
                .add(13, 13, 13)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(removeButton)
                    .add(closeButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			nameTF.setEnabled(true);
			nameTF.setEditable(true);
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			this.machine.setName(nameTF.getText());
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
	}//GEN-LAST:event_editButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil slette "+this.machine.getName()+"?", "Bekræftelse af sletning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.removeMachine(machine);
			this.dispose();
		}
	}//GEN-LAST:event_removeButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.ButtonGroup availabilityBG;
    private javax.swing.JLabel availabilityLabel;
    private javax.swing.JRadioButton availableNoRB;
    private javax.swing.JRadioButton availableYesRB;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables

}
